//
//  HCustomScrollView.m
//  HB
//
//  Created by 陶秋安 on 16/5/19.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "HCustomScrollView.h"

@implementation HCustomScrollView

- (void)loadImageData
{
    _imageCount = _mArrayImageData.count;
    
    [self downloadImg];
    
    [self addImageViewsToScrollView];
}

-(void) downloadImg
{
    
    for (NSInteger i=0; i<[_mArrayImageData count]; i++)
    {
        NSDictionary *dict= [_mArrayImageData objectAtIndex:i];
        
        NSString *imgName=[dict objectForKey:@"PathName"];
        
        NSString *imgPath= [dict objectForKey:@"Path"];
        
        if(![Common checkLanchExist:imgName])
        {
            
            NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgPath]];
            
            [data writeToFile:[FxGlobal getCacheImage:imgName] atomically:YES];
            
        }
        
    }
}

- (void)addScrollView
{
    
    _scrV = [[UIScrollView alloc] init];
    
    _scrV.translatesAutoresizingMaskIntoConstraints=NO;
    
    _scrV.pagingEnabled = NO;
    
    _scrV.showsHorizontalScrollIndicator = NO;
    
    _scrV.delegate = self;
    
    
    [self addSubview:_scrV];
    
    //ImageView左侧与父视图左侧对齐
    NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:_scrV attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
    [self addConstraint:leftConstraint];
    
    //ImageView右侧与父视图右侧对齐
    NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:_scrV attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0f constant:0.0f];
    [self addConstraint:rightConstraint];
    
    //ImageView顶部与父视图顶部对齐
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:_scrV attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self  attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
    [self addConstraint:topConstraint];
    
    //ImageView底部与父视图顶部对齐
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:_scrV attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
    [self addConstraint:bottomConstraint];
    
}

- (void)addImageViewsToScrollView
{
    dispatch_async(dispatch_get_main_queue(), ^(void){
        [_scrV removeAllSubviews];
    });
    
    for (NSInteger i=0; i<[_mArrayImageData count]; i++)
    {
        //图片视图；中间
        UIImageView *_imgVCenter = [[UIImageView alloc]init];
        
        _imgVCenter.translatesAutoresizingMaskIntoConstraints=NO;
        
        _imgVCenter.contentMode = UIViewContentModeScaleAspectFit;
        
        NSDictionary *dict= [_mArrayImageData objectAtIndex:i];
        
        NSString *imgName =[dict objectForKey:@"PathName"];
        
        NSString *filePath = [FxGlobal getCacheImage:imgName];
        
         UIImage * img= [UIImage imageWithContentsOfFile:filePath];
        
        [_imgVCenter setImage:img];
        
        dispatch_async(dispatch_get_main_queue(), ^(void){
            
            [_scrV addSubview:_imgVCenter];
            
            [self setPostion:i andView:_imgVCenter andHost:_scrV];
            
        });
        
    }
    
}

-(void) setPostion:(NSInteger) i andView:(UIView *) view andHost:(UIView *)host
{
    
    
    if (i==0) {
        //ImageView左侧与父视图左侧对齐
        NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:host attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
        [host addConstraint:leftConstraint];
    }
    //ImageView顶部与父视图顶部对齐
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:host attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
    
    //ImageView底部与父视图顶部对齐
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:host attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
    
    //ImageView高度为父视图高度
    NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:187.0f];
    
    //ImageView宽度为父视图宽度
    NSLayoutConstraint* widthConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:187.0f];
    
    //iOS 6.0或者7.0调用addConstraints
    [host addConstraints:@[bottomConstraint,topConstraint,heightConstraint,widthConstraint]];
    
    if(i>0)
    {
        //imageView 右边距和父视图对齐 以便确定父视图的内容边界
        UIImageView *imgView=(UIImageView * )host.subviews[i-1];
        
        NSLayoutConstraint *spaceConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:imgView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:5.0f];
        
        [host addConstraint:spaceConstraint];
    }
    if(i==_imageCount-1)
    {
        
        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:host attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
        
        [host addConstraint:constraint];
    }
    
}

- (void)layoutUI
{
    [self addScrollView];
}

-(void)loadData:(NSArray *) paramArrayImageData
{
    
    _mArrayImageData= paramArrayImageData;
    
    [self loadImageData];
    
}

#pragma mark 设置滚动视图的位置
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    CGPoint targetOffset = [self nearestTargetOffsetForOffset:*targetContentOffset];
    
    targetContentOffset->x = targetOffset.x;
    
    targetContentOffset->y = targetOffset.y;
}

#pragma mark 计算滚动视图的点
-(CGPoint) nearestTargetOffsetForOffset:(CGPoint) offset
{
    CGFloat pageSize = 192.0;
    
    NSInteger page = roundf(offset.x / pageSize);
    
    CGFloat targetX = pageSize * page;
    
    return CGPointMake(targetX, offset.y);
}

@end
